import fetch from "node-fetch";
import YAML from "yaml";
import {createRequire} from "module";
const require = createRequire(import.meta.url);
const fs =require('fs')
const _path = process.cwd();
import { Config } from '../components/index.js'

let ing = {};

export class duel extends plugin {
constructor() {
super({
name: '话痨统计',
dsc: '看看谁在水',
event: 'message',
priority: 2000,
rule: [
{
reg: "^#(话痨统计|话唠统计|水群统计|氵群统计)$",
fnc: 'FuckingChatterbox'
}
]
})
}

async FuckingChatterbox(e) {
let set = await Config.admin_set
let isplugin = set.carp
if (!isplugin) return false
if(ing[e.group_id]){
e.reply("在找了，在找了，已经在找了，很慢，请再等等！");
return true;
}
ing[e.group_id] = 1;
e.reply("正在分析聊天记录，寻找本群大水逼，请等一等！");
let CharHistory = await e.group.getChatHistory(0, 1);
let seq = CharHistory[0].seq;
e.reply(`大概有${seq}条记录需要分析，预计需要${(seq/20/4/60).toFixed(2)}分钟`);
let CharList = {};
let allcount = 0;
console.log("seq:",seq);
for (let i = seq; i > 0; i = i - 20) {
let CharTemp = await e.group.getChatHistory(i, 20);
if (CharTemp.length == 0) {
break;
}
for (const key in CharTemp) {
if (CharTemp[key].length == 0) {
continue;
}
if(CharTemp[key].user_id == Bot.uin){
continue;
}
allcount ++;
if (CharList[CharTemp[key].user_id]) {
CharList[CharTemp[key].user_id].times += 1;
CharList[CharTemp[key].user_id].uname = CharTemp[key].sender.card ? CharTemp[key].sender.card : CharTemp[key].sender.nickname;
} else {
CharList[CharTemp[key].user_id] = {
times: 1,
user_id: CharTemp[key].user_id,
uname: CharTemp[key].sender.card ? CharTemp[key].sender.card : CharTemp[key].sender.nickname
};
}
}
}
let CharArray = [];
for (const key in CharList) {
CharArray.push(CharList[key]);
}
CharArray.sort((a, b) => {
return b.times - a.times
})
console.log(CharArray);
let l = Math.ceil(CharArray.length / 10);
CharArray = CharArray.slice(0, l > 10 ? 10 : l);
let res = `一共检测到聊天记录 ${allcount} 句话，其中：\n`;
let itemp = 0;
for (let v of CharArray) {
res += (v.uname) + `  水了${v.times}句话，占比${(v.times/allcount*100).toFixed(2)}% `;
if(!itemp){
res += " （这人水是真滴多 ！）";
}
if(itemp != CharArray.length -1 ){
res += "\n";
}
itemp++;
}
e.reply(res);
ing[e.group_id] = 0;
return true; //返回true 阻挡消息不再往下
}
}